<html>
<head><meta charset="utf-8"><title>How do I get an Item from a DefId? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html">How do I get an Item from a DefId?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199991110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20an%20Item%20from%20a%20DefId%3F/near/199991110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html#199991110">(Jun 06 2020 at 20:09)</a>:</h4>
<p>Sort of the opposite of <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/How.20do.20I.20get.20the.20module.20that.20an.20Item.20belongs.20to.3F">https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/How.20do.20I.20get.20the.20module.20that.20an.20Item.20belongs.20to.3F</a> - I have a <code>rustc_hir::DefId</code> and I want to get a <code>rustc_hir::Item</code>. Is there a way to do that? In case it helps, I don't need the whole <code>Item</code>, just the <code>ItemKind</code>.</p>



<a name="199991503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20an%20Item%20from%20a%20DefId%3F/near/199991503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html#199991503">(Jun 06 2020 at 20:21)</a>:</h4>
<p>You can't get the <code>ItemKind</code> for cross-crate items. Is <code>tcx.def_kind</code> good enough for what you need?</p>



<a name="199992030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20an%20Item%20from%20a%20DefId%3F/near/199992030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html#199992030">(Jun 06 2020 at 20:35)</a>:</h4>
<p>I think I found it going another way, rustdoc stores an <code>ImportSource</code> which I think has the <code>DefId</code> of the item being imported: <a href="https://github.com/rust-lang/rust/blob/master/src/librustdoc/clean/types.rs#L1500">https://github.com/rust-lang/rust/blob/master/src/librustdoc/clean/types.rs#L1500</a></p>



<a name="199992093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20an%20Item%20from%20a%20DefId%3F/near/199992093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html#199992093">(Jun 06 2020 at 20:37)</a>:</h4>
<p>Are <code>HirId</code>s only for the current crate? Or can they be cross-crate?</p>



<a name="199992143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20an%20Item%20from%20a%20DefId%3F/near/199992143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html#199992143">(Jun 06 2020 at 20:38)</a>:</h4>
<p>Only the current crate</p>



<a name="199992178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20an%20Item%20from%20a%20DefId%3F/near/199992178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20an.20Item.20from.20a.20DefId.3F.html#199992178">(Jun 06 2020 at 20:40)</a>:</h4>
<p>Got it, so this is why <code>resolve_str_path_error</code> only works on local items. Thanks.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>